package SiTuQingYou.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oreilly.servlet.MultipartRequest;

import SiTuQingYou.dao.CommentDao;
import SiTuQingYou.dao.LikeCollectDao;
import SiTuQingYou.dao.StrategyDao;
import SiTuQingYou.util.Pager;
import SiTuQingYou.vo.Comment;
import SiTuQingYou.vo.Scene;
import SiTuQingYou.vo.Strategy;
import SiTuQingYou.vo.User;
import SiTuQingYou.util.FileRename;

/**
 * Servlet implementation class StrategyServlet
 */
@WebServlet("/StrategyServlet")
public class StrategyServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StrategyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void getStrategyContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String sid=request.getParameter("id");
		 int id=Integer.parseInt(sid);
		 Strategy strategy=StrategyDao.getsd().getContentByStrategyId(id);
	     request.setAttribute("strategy", strategy);
		 
	}
	protected void getAllStrategy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//所有的攻略
		 Pager pager =  Pager.init(request);
		 ArrayList<Strategy> strategys=StrategyDao.getsd().getAllStrategy(pager);
		 request.setAttribute("strategys", strategys);
		
	}
	protected void getAllStrategys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//分页
		Integer pagenum=1;
		try {
				pagenum = Integer.parseInt(request.getParameter("pagenum"));
		}
		catch (Exception e) {
			
		}
		 Pager  pager =  Pager.init(request);
		 ArrayList<Strategy> strategys=StrategyDao.getsd().findAllStrategy(pagenum);
		  int datacount = StrategyDao.getsd().getcout();
		  int abnum =   StrategyDao.getsd().allauditing();
		  request.setAttribute("abnum", abnum);
		 request.setAttribute("strategys", strategys);
		request.setAttribute("datacount",datacount);
	}
	
	
	protected void alllike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//所有的点赞收藏
		Integer a =  Integer.parseInt(request.getParameter("lovenum"));
		int  likenum=LikeCollectDao.getlcd().getlike(a);
		int collectnum = LikeCollectDao.getlcd().getcoullct(a);
		
		request.setAttribute("collectnum", collectnum);
		request.setAttribute("likenum", likenum);
		
	}
	protected void getThreeStrategy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//չʾ����
		 ArrayList<Strategy> strategys=StrategyDao.getsd().getThreeStrategy();
		 request.setAttribute("strategys", strategys );
		
	}
	protected void getAllStrategyBySceneId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//չʾ����
		 String sid=request.getParameter("id");
		 int id=Integer.parseInt(sid);
		 ArrayList<Strategy> strategys=StrategyDao.getsd().getAllStrategyBySceneId(id);
		 request.setAttribute("strategys", strategys );
		  
	}

	protected void allcomment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr//	
		int stratrgyid = Integer.parseInt(req.getParameter("cid"));
      ArrayList<Comment> comment =CommentDao.getcd().getallcomment(stratrgyid);
		req.setAttribute("comment",comment);
		req.getRequestDispatcher("/houtai/allcomment.jsp").forward(req, resp);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		
	}
	//接收提交的攻略表单，并存入数据库
	protected void writeStrategy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String filepath=req.getServletContext().getRealPath("/");
		MultipartRequest re= new MultipartRequest(req,filepath+"/imagess/",2*1024*1024,"utf-8",new SiTuQingYou.util.FileRename());
		Strategy strategy=new Strategy();
		Scene scene=new Scene();
		scene.setId((Integer.parseInt(re.getParameter("sceneid"))));
		strategy.setScene(scene);
		strategy.setTitle(re.getParameter("title"));
		HttpSession session=req.getSession();
		strategy.setUser((User)session.getAttribute("user"));
		strategy.setContent(re.getParameter("content"));
		strategy.setPhoto(re.getFilesystemName("cover"));
		StrategyDao.getsd().writeStrategy(strategy);
		resp.sendRedirect(this.getServletContext().getContextPath()+"/index/index.jsp");
		
	}
	
	
	//检查用户是否登录，登录返回1,否则返回0
	protected void checkLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 HttpSession session=req.getSession();
		 User user=(User) session.getAttribute("user");
		 PrintWriter out=resp.getWriter();
		 if(user==null)
		 {
			 out.print("0");
		 }
		 else
		 {
			 out.print("1");
		 }
		
	}
	
	protected void deleteone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		int id = Integer.parseInt(req.getParameter("cid"));
		StrategyDao.getsd().deletestr(id);
		
	}
	protected void showallaub(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		ArrayList<Strategy> str= StrategyDao.getsd().showallaud();
		req.setAttribute("str", str);
	}
	protected void passornopass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		int a  = Integer.parseInt(req.getParameter("c"));
		int id = Integer.parseInt(req.getParameter("cid"));
		String name = req.getParameter("yuanyin");
		 StrategyDao.getsd().passstr(a,id,name);  
		  int abnum =   StrategyDao.getsd().allauditing();
		  PrintWriter out = resp.getWriter();
	    	out.print(abnum);
	}
	
	//获得此用户写的所有攻略（审核通过的和未通过的都显示）
	protected void showAllWritedStrategy(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session=req.getSession();
		User user=(User) session.getAttribute("user");
		Pager  pager =  Pager.init(req);
		ArrayList<Strategy> writedstr = StrategyDao.getsd().getAllWritedStrategyByUser(user.getId(),pager);
		req.setAttribute("writedstr",writedstr);
	}
}
